package com.huarui.initialItem.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.initial.model.Initial;
import com.huarui.initial.service.InitialService;
import com.huarui.initial.util.InitialUtil;
import com.huarui.initial.util.InitialVO;
import com.huarui.initialItem.service.InitialItemService;
import com.huarui.initialItem.util.InitialItemVO;
import com.huarui.part.service.PartService;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.opensymphony.xwork2.Action;

@SuppressWarnings("serial")
public class InitialItemAction extends BaseAction {
	private InitialItemService initialItemService;
	private InitialService initialService;
	private PartService partService;

	// 分页信息参数
	private Integer page;
	private Integer pageSize;
	private String orderBy;
	private String sord;
	private String sort;//排序字段
	private String dir; //排序方向
	
	
	private Integer start;
	private Integer limit;
	
	private String data;
	// 对象参数
	private Integer id;
	private Integer initialId; //  期初单ID`

	private String code;	// 编码(冗余字段) NOT NULL
	private Integer source;//来源(原厂，合作厂中国，合作厂进口，非合作厂中国，非合作厂进口)
	
	private String searchConditions;
	
	private String operation;
	private Initial initial;
	private InitialVO initialVO;
	private ListModel<InitialItemVO> listModel;
	SessionMessage sm = UserHolder.getCurrentSessionMessage();
	Map<String,Object> resultMap=new HashMap<String, Object>();

	public SessionMessage getSm() {
		return sm;
	}

	public void setSm(SessionMessage sm) {
		this.sm = sm;
	}
	
	public String lookUp(){
		return "lookUp";
	}

    public String extList(){
	    ListModel<InitialItemVO> lm = new ListModel<InitialItemVO>();
	    sm=UserHolder.getCurrentSessionMessage();
	    lm.setPageSize(limit==null?10:limit);
	    lm.setPage(start==null?0:start/limit+1);
		lm.setOrderBy(sort==null?"id":sort);
		if (dir != null){
			lm.setDesc(dir.equals("DESC") ? true : false);
		}else{
			lm.setDesc(true);
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("companyId", sm.getCompanyId());
		map.put("initialId", initialId);
		if(searchConditions!=null){
		List<String> conditions = new ArrayList<String>();
		InitialUtil.addToCollection(conditions, InitialUtil.split(searchConditions, " ,"));
		map.put("conditions",conditions);
		}
		if(id!=null){
			map.put("id",id);
		}
		initialItemService.getInitialItemsByInitialId(map, lm);			
		resultMap.put("totalProperty", lm.getTotal());
		resultMap.put("root", lm.getResult());
		return Action.SUCCESS;
    }
    public String query(){
	    ListModel<InitialItemVO> lm = new ListModel<InitialItemVO>();
	    sm=UserHolder.getCurrentSessionMessage();
	    lm.setPageSize(limit==null?8:limit);
	    lm.setPage(start==null?0:start/limit+1);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("initialId", initialId);
		if(searchConditions!=null||!"".endsWith(searchConditions)){
			map.put("searchConditions",searchConditions);
		}

		initialItemService.query(map, lm);			
		resultMap.put("totalProperty", lm.getTotal());
		resultMap.put("root", lm.getResult());
		return Action.SUCCESS;
    }
	public String delete(){
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=initialItemService.deleteInitialItemById(id,initialId,sm);
		return Action.SUCCESS;
	}

	public InitialItemService getInitialItemService() {
		return initialItemService;
	}

	public void setInitialItemService(InitialItemService initialItemService) {
		this.initialItemService = initialItemService;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public Integer getInitialId() {
		return initialId;
	}

	public void setInitialId(Integer initialId) {
		this.initialId = initialId;
	}

	public InitialService getInitialService() {
		return initialService;
	}

	public void setInitialService(InitialService initialService) {
		this.initialService = initialService;
	}

	public ListModel<InitialItemVO> getListModel() {
		return listModel;
	}

	public void setListModel(ListModel<InitialItemVO> listModel) {
		this.listModel = listModel;
	}

	public Initial getInitial() {
		return initial;
	}

	public void setInitial(Initial initial) {
		this.initial = initial;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Integer getSource() {
		return source;
	}

	public void setSource(Integer source) {
		this.source = source;
	}




	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}


	public PartService getPartService() {
		return partService;
	}

	public void setPartService(PartService partService) {
		this.partService = partService;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public InitialVO getInitialVO() {
		return initialVO;
	}

	public void setInitialVO(InitialVO initialVO) {
		this.initialVO = initialVO;
	}

	public String getSearchConditions() {
		return searchConditions;
	}

	public void setSearchConditions(String searchConditions) {
		this.searchConditions = searchConditions;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	

}
